Open
Conversation
Fity
reviewed
May 27, 2017
| **关注用户和被关注用户** | ||
|
|
||
| ``` | ||
| >>> zhihu.follows("高日日") |
| url = 'https://www.zhihu.com/search?type=people&q={}'.format(Name) | ||
| data = requests.get(url, headers=Headers) | ||
| soup = BeautifulSoup(data.text, 'lxml') | ||
| yonghu = soup.select('a[class="name-link author-link"]') |
| yonghu = soup.select('a[class="name-link author-link"]') | ||
| pattern = '<a.*?href="([^"]*)".*?>(?:[\S\s]*?)</a>' | ||
| token = None | ||
| for i in yonghu: |
There was a problem hiding this comment.
两个点。
- 既然下面用
for来迭代了,这里显然应该是用复数:users,user_list之类的东西。 for循环中,用i通常都是index的缩写,这里显然是对应的user对象。所以,user就好。for user in users: pass
| i = str(i) | ||
| mat = re.findall(pattern, i) | ||
| token = mat | ||
| if token[0]: |
There was a problem hiding this comment.
这里的逻辑是,找到第一个 match(对,mat 也没几个人知道是啥,不要随便缩写。。。)然后直接 return match[0]。所以,其实 token 这个变量完全没有存在的意义。下面这样写就够了。
for user in users:
match = re.findall(pattern, str(user))
if match[0]:
return match[0]
return None
|
|
||
| >>> follows(user_name = "高日日") | ||
| """ | ||
| if not user_name: |
|
|
||
| user_slug = self._get_token(user_name) | ||
|
|
||
| response = requests.get(URL.user_followed_number(user_slug), headers={'User-Agent': |
There was a problem hiding this comment.
headers 的值应该有固定的?如果没有的话,找个地方放一下,然后在这里(以及其他地方)引用
|
|
||
| dicts = dict() | ||
| counts = 0 | ||
| for i in init_data_renshu: |
There was a problem hiding this comment.
这里直接写两遍 i.get_next() 就可以了吧。。。没必要用 for 循环,看着很别扭。还强行设置了一个 counts...
顺便,小伙子你大概不知道 enumerate 这个东西?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
更改了4个文件:
1.README
2.url.py
3.zhihu/models/init.py
4.zhihu/models/common.py